﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Mask;

namespace ProgNetComponentsDx.Forms
{
    public partial class InputForm : XtraForm
    {
        /// <summary>
        /// Shows input dialog and disposes the form
        /// </summary>
        /// <param name="text">Label text</param>
        /// <param name="caption">Forms text</param>
        /// <param name="mask">Mask</param>
        /// <param name="maskType">Mask type</param>
        /// <param name="validation">Validation</param>
        /// <returns></returns>
        public static string InputDialog(string text, string caption, string mask, MaskType maskType = MaskType.RegEx, ObjectValidationEventHandler validation = null)
        {
            InputForm f = new InputForm();
            f.InputMaskType = maskType;
            f.InputMask = mask;
            if (validation != null)
                f.ValidateValue += new ObjectValidationEventHandler(validation);
            string str = f.ShowInputDialog(text, caption);
            f.Dispose();
            return str;
        }

        public InputForm()
        {
            InitializeComponent();
        }

        public string InputMask
        {
            get { return textEdit1.Properties.Mask.EditMask; }
            set { textEdit1.Properties.Mask.EditMask = value; }
        }
        public DevExpress.XtraEditors.Mask.MaskType InputMaskType
        {
            get { return textEdit1.Properties.Mask.MaskType; }
            set { textEdit1.Properties.Mask.MaskType = value; }
        }

        private string _resStr;
        public string ResultString
        {
            get { return _resStr; }
        }
        private bool _ValidateOnChange;
        public bool ValidateOnChange
        {
            get { return _ValidateOnChange; }
            set
            {
                if (_ValidateOnChange != value)
                {
                    _ValidateOnChange = value;
                    if (value)
                        textEdit1.EditValueChanging += new DevExpress.XtraEditors.Controls.ChangingEventHandler(textEdit1_EditValueChanging);
                    else
                        textEdit1.EditValueChanging -= textEdit1_EditValueChanging;
                }
            }
        }

        void textEdit1_EditValueChanging(object sender, DevExpress.XtraEditors.Controls.ChangingEventArgs e)
        {
            OnValidateText(e.NewValue.AsString(), true);
        }
        public string ShowInputDialog(string text, string caption)
        {
            Text = caption;
            lbl.Text = text;
            string s = null;
            if (ShowDialog() == DialogResult.OK)
                s = ResultString;
            return s;
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            _resStr = textEdit1.Text;
            if (OnValidateText(_resStr, false))
            {
                DialogResult = DialogResult.OK;
            }
        }

        public event ObjectValidationEventHandler ValidateValue;
        
        private bool OnValidateText(string text, bool errorOnly)
        {
            if (ValidateValue != null)
            {
                ObjectValidateEventArgs e = new ObjectValidateEventArgs(text);
                e.ErrorOnly = errorOnly;

                ValidateValue(this, e);
                string err = e.ValidationErrorMsg;
                    dxErrorProvider1.SetError(textEdit1,err);
                return err.IsNullOrEmpty();
            }
            else
                return true;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            _resStr = null;
            DialogResult = DialogResult.Cancel;
        }
    }

    public delegate void ObjectValidationEventHandler(object sender, ObjectValidateEventArgs e);
    public class ObjectValidateEventArgs : EventArgs
    {
        public ObjectValidateEventArgs(object value)
        {
            Value = value;
            ErrorOnly = true;
        }
        public bool ErrorOnly
        {
            get;
            set;
        }
        public string ValidationErrorMsg
        {
            get;
            set;
        }
        public object Value
        {
            get;
            private set;
        }
    }

}
